package com.hama.iotrust.common.mvc;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class WebSessionListener implements HttpSessionListener {

    private static final Logger logger = LoggerFactory.getLogger(WebSessionListener.class);

    /**
     * session 创建时触发
     */
    @Override
    public void sessionCreated(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        logger.debug("会话创建 session = {}", session.getId());

        synchronized (this) {
            WebSessionManager.getInstance().add(session);
        }
    }

    /**
     * session 销毁时触发
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        logger.debug("会话销毁 session = {}", session.getId());
        synchronized (this) {
            WebSessionManager.getInstance().remove(session);
        }
    }

}
